/**
 * 
 */
package View;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import Controllor.DB;
import Model.Lease;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.SwingConstants;

/**
 * This class is a the interface for a list of leases in an apartment complex. 
 * 
 * @author Ryan Jeffries
 * @version 6/3/2014
 *
 */
public class Leases extends JPanel implements Observer {
	private JTable table;

	/**
	 * Column headers
	 */
	private String[] TABLE_HEADERS = {"Start Date", "Lease Term", "Deposit Paid", "Discount", "Pets", "Pet Deposit Paid", "Insurance Carrier", "Insurance ID", "Apartment Number", "Leased By", "Resident ID"};
	
	/**
	 * Database which connects to sql.
	 */
	private DB database;
	
	/**
	 * List of leases.
	 */
	private List<Lease> leases;
	
	/**
	 * Currently selected 
	 */
	private int selected_row;
	
	/**
	 * Initializes leases.
	 */
	public Leases(final DB database) {
		super();
		
		// Initialize variables
		this.database = database;
		this.leases = database.getLeases();
		
		
		setLayout(new BorderLayout(0, 0));
		start();
  		updateTable();
	}
	
	/**
	 * Helper method for leases constructor
	 */
	private void start() {
		// Adds Apartment Label
		JPanel labelPanel = new JPanel();
		add(labelPanel, BorderLayout.NORTH);
		labelPanel.setLayout(new GridLayout(0, 4, 0, 0));
		JLabel label = new JLabel("Leases");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setFont(new Font("Tahoma", Font.PLAIN, 15));
		labelPanel.add(label);
			
		JButton btnNewButton = new JButton("New Lease");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new LeaseForm(database);
			}
		});
		labelPanel.add(btnNewButton);
		JButton btnNewButton_1 = new JButton("Edit Lease");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (selected_row > -1)
					new LeaseForm(leases.get(selected_row), database, true);
				//new LeaseGUI(leases.get(selected_row));				
				//Apartment a = database.getApartment(startDate, apartmentNumber);
			}
		});
		labelPanel.add(btnNewButton_1);
		JButton btnNewButton_2 = new JButton("Delete Lease");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (selected_row > -1) {					
					String startDate = (String) table.getModel().getValueAt(selected_row, 0);
					String apartmentNumber = (String) table.getModel().getValueAt(selected_row, 8);		
					leases.remove(selected_row);
					selected_row = -1;
					database.removeLease(startDate, apartmentNumber);
					updateTable();
				}
			}
		});
		labelPanel.add(btnNewButton_2);
		// Adds Scroll Pane
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		// Adds JTable
		table = new JTable() {
		    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
		        Component c = super.prepareRenderer(renderer, row, column);
		        if (row == selected_row) {		        
		        	c.setBackground(new Color(245, 245, 220));
		        } else {
		        	 c.setBackground(getBackground());
		        }
		        return c;
		    }
		    @Override
		    public boolean isCellEditable(int row, int column) {
		        return false;
		    }
		};
		Object[][] o = new Object[100][7];
		table.setModel(new DefaultTableModel(o, TABLE_HEADERS));
		table.setEnabled(false);
		scrollPane.setViewportView(table);
  		table.addMouseListener(new MouseAdapter() {
   			public void mouseClicked(MouseEvent e) {
      			if (e.getClickCount() == 1) {
         			selected_row = table.rowAtPoint(e.getPoint());
        			updateTable();
         		}
   			}
		});
	}
	
	/**
	 * Updates table content
	 */
	public void updateTable() {	
		leases = database.getLeases();
		Object[][] o = new Object[leases.size()][11];
		for (int i = 0; i < leases.size(); i++) {
			o[i][0] = leases.get(i).getStartDate();
			o[i][1] = leases.get(i).getTerm();
			o[i][2] = leases.get(i).getDepositPaid();
			o[i][3] = leases.get(i).getMonthlyDiscount();
			o[i][4] = leases.get(i).getNumberOfPets();
			o[i][5] = leases.get(i).getPetDepositPaid();
			o[i][6] = leases.get(i).getRentersInsuranceCarrier();
			o[i][7] = leases.get(i).getRentersInsuranceID();
			o[i][8] = leases.get(i).getApartmentNumber();
			o[i][9] = leases.get(i).Employee_EmployeeID;
			o[i][10] = leases.get(i).Resident_ResidentID;
		}
		table.setModel(new DefaultTableModel(o, TABLE_HEADERS));
	}
	
	@Override
	public void update(Observable arg0, Object arg1) {		
		updateTable();
	}

}